<template>
  <div>
    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" max-height="250" size="mini" border @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" :selectable="selectable"></el-table-column>
      <el-table-column prop="student_title" label="名称" align="center">
        <template slot-scope="scope">
          <span v-if="scope.$index === 0">
            <el-input v-model="title" placeholder="请输入名称" size="mini"></el-input>
          </span>
          <span v-else> {{scope.row.student_title}} </span>
        </template>
      </el-table-column>
      <el-table-column prop="topics_num" label="题量" align="center"> </el-table-column>
      <el-table-column prop="count" label="时长" width="120" align="center"> </el-table-column>
      <el-table-column prop="operation" label="操作" align="center">
        <template slot-scope="scope">
          <template v-if="scope.$index ===0">
            <el-button type="primary" round size="mini" @click="examine('search',scope.row)">查询</el-button>
            <el-button type="text" @click="examine('remove',scope.row)">清除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import {APISINGLELIST} from '@/common/api.js'
export default {
  props: ['singleList'],
  data () {
    return {
      title: '',
      seacherTab: {
        student_title: '',
        topics_num: '',
        count: ''
      },
      tableData: [],
      totalNumber: Number
    }
  },
  created () {
    this.tableList()
  },
  methods: {
    selectable (row, index) {
      if (index !== 0) {
        return true
      } else {
        return false
      }
    },
    tableList () {
      APISINGLELIST({
        title: this.title
      }).then((res) => {
        if (res.status === 1) {
          this.tableData = res.data.list
          this.tableData.splice(0, 0, this.seacherTab)
          this.totalNumber = res.data.totalNumber
          this.toggleSelection(this.singleList)
        } else {
          this.$message.error(res.msg)
          this.tableData = []
          this.tableData.push(this.seacherTab)
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    examine (type, e) { // 操作按钮
      if (type === 'search') {
        this.tableList()
      } else if (type === 'remove') {
        this.title = ''
        this.tableList()
      }
    },
    toggleSelection (rows) {
      if (rows) {
        this.$nextTick(() => {
          rows.forEach(row => {
            this.tableData.forEach((item, index) => {
              if (row.id === item.id) {
                this.$refs.multipleTable.toggleRowSelection(this.tableData[index])
              }
            })
          })
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.$emit('haveChosen', val, this.totalNumber)
    }
  }
}
</script>
